import { useEffect, useState } from 'react'
import { getNormalUserListApi, UserDel, Authentication, getCheckboxListApi } from '@/apis/user'
import { message } from 'antd'

//获取用户列表信息
function useUserList() {
    //获取文章列表
    const [userInfoList, setUserInfoList] = useState([])
    //空文本
    const [emptyText, setEmptyText] = useState('No data')
    //获取文章列表参数
    const [reqData, setReqData] = useState({
        page: 1,
        MaxResultCount: 10,
        // sp_role: null,
        // permission: null,
        start_time: null,
        end_time: null,
        key: '',
        SkipCount: 0,
        current: 1,
        pageSize: 10,
        showSizeChanger: true,
        pageSizeOptions: ['10', '20', '40', '60'],
    })
    //行总数
    const [count, setCount] = useState(0)
    //用户是否有权限
    const [authentication, setAuthentication] = useState({})
    //获取矿山列表
    const [checkboxList, setCheckboxList] = useState([])

    const [userToken, setUserToken] = useState({})

    //获取筛选数据
    const onFinish = (formValue) => {
        // console.log(formValue)
        // 1. 准备参数
        const { ceate_time } = formValue
        // console.log(permission)
        setReqData({
            ...reqData,
            page: 1,
            current: 1,
            // per_page: 10,
            // MaxResultCount: 10,
            // permission: permission,
            // sp_role: _sp_role,
            // key: key,
            SkipCount: 0,
            start_time: ceate_time == undefined ? null : ceate_time[0].format('YYYY-MM-DD'),
            end_time: ceate_time == undefined ? null : ceate_time[1].format('YYYY-MM-DD'),
        })
    }
    //获取异步数据
    const getList = async function getList() {
        //用户鉴权
        // const auth_res = await Authentication()
        // setAuthentication(auth_res)
        // console.log(auth_res)
        const res = await getNormalUserListApi(reqData)
        setCount(res.data.totalCount)
        // console.log(res.data.items)
        setUserToken(res.userToken)
        setUserInfoList(res.data.items)
    }

    //用户列表列表
    useEffect(() => {
        getList()
    }, [reqData])

    //列表分页
    const onPageChange = (val) => {
        // console.log(val)
        // return
        setReqData({
            ...reqData,
            current: val.current,
            pageSize: val.pageSize,
            MaxResultCount: val.pageSize,
            SkipCount: (val.current - 1) * val.pageSize
        })
    }
    //删除数据
    const onConfirm = async (id) => {
        const res = await UserDel(id)
        if (res.code == 200) {
            message.success("删除成功")
            getList()
        }
        else {
            message.error(res.message)
        }
    }

    // //获取矿山列表
    // useEffect(() => { getCheckboxList() }, [])
    // //获取矿山列表
    // const getCheckboxList = async () => {
    //     const res = await getCheckboxListApi()
    //     setCheckboxList(res.data)
    // }
    return { authentication, emptyText, reqData, count, userInfoList, checkboxList, userToken, onConfirm, onPageChange, onFinish, getList, setEmptyText }
}

export default useUserList